#include<iostream>
#include<vector>
#include<unordered_map>
#include<algorithm>
#include<map>
#include<set>
#include<unordered_set>
#include<bits/stdc++.h>
#include<ext/pb_ds/priority_queue.hpp>
//#include<bits/extc++.h>
using namespace std;
//using namespace __gnu_pbds;

#define fi first
#define se second
#define SIZE(x) ((int)x.size())
#define out(x) cerr<<#x<<" = "<<(x)<<endl;
#define MP(a,b) make_pair(a,b)
typedef long long ll;
#define VI vector<int>
#define VVI vector< vector<int> >
#define VL vector<ll>
#define VVL vector<VL >
#define VC vector<char>
#define PI pair<int,int>
#define PL pair<ll,ll>
#define EPS 0.0000001


inline int lson(int x){return x<<1;}
inline int rson(int x){return (x<<1)|1;}

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

template<typename T> 
inline void read(T &x){
	x=0;T f=1;char ch;
	do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
	do x=x*10+ch-'0',ch=getchar();while(ch<='9'&&ch>='0');x*=f;
}

//#define INF 5340172838076674L
#define INF 1000000000
//#define MOD 998244353
//#define W 20

const int maxn=1000010;
int na,tot=0,root;
int ch[maxn][2]; //0是左儿子，1是右儿子
int val[maxn],dat[maxn];//val是值，dat是随机优先级
int siz[maxn],cnt[maxn];//size是子树大小，cnt是每个节点的副本个数
//创建新节点
int New(int v){
	val[++tot]=v;
	dat[tot]=rand();
	siz[tot]=1;
	cnt[tot]=1;
	return tot;
}
void Pushup(int id){
	siz[id]=siz[ch[id][0]]+siz[ch[id][1]]+cnt[id];
}
//构建Treap
void Build(){
	memset(ch,0,sizeof ch);
	root=New(-INF);ch[root][1]=New(INF);
	Pushup(root);
}
//id是引用，d=0表示左旋，d=1表示右旋
void Rotate(int &id,int d){
	int tmp=ch[id][d^1];
	ch[id][d^1]=ch[tmp][d];
	ch[tmp][d]=id;
	id=tmp;
	Pushup(ch[id][d]);
	Pushup(id);
}
//插入值为v的节点
void Insert(int &id,int v){
	if(!id){
		id=New(v);
		return;
	}
	if(v==val[id])cnt[id]++;
	else{
		int d=v<val[id]?0:1;
		Insert(ch[id][d],v);
		if(dat[id]<dat[ch[id][d]])Rotate(id,d^1);
	}
	Pushup(id);
}
//删除一个值为v的节点
void Remove(int &id,int v){
	if(!id)return;//找不到节点
	if(v==val[id]){
		if(cnt[id]>1){cnt[id]--;Pushup(id);return;}
		if(ch[id][0]||ch[id][1]){
			//如果cnt=1并且有儿子节点，我们需要把它旋转到底部再删除
			if(!ch[id][1]||dat[ch[id][1]]<dat[ch[id][0]]){
				//优先级大的补上来
				Rotate(id,1);Remove(ch[id][1],v);
			}else{ Rotate(id,0);Remove(ch[id][0],v);}
			Pushup(id);
		}else id=0;
		return;
	}
	v<val[id]?Remove(ch[id][0],v):Remove(ch[id][1],v);
	Pushup(id);
}
//查询排名（别忘了还有-INF存在
int GetRank(int id,int v){
	if(!id)return 0;
	if(v==val[id])return siz[ch[id][0]]+1;
	else if(v<val[id])return GetRank(ch[id][0],v);
	else return siz[ch[id][0]]+cnt[id]+GetRank(ch[id][1],v);
}
//查询rank的元素
int GetVal(int id,int rank){
	if(!id)return INF;
	if(rank<=siz[ch[id][0]])return GetVal(ch[id][0],rank);
	else if(rank<=siz[ch[id][0]]+cnt[id])return val[id];
	else return GetVal(ch[id][1],rank-siz[ch[id][0]]-cnt[id]);
}
//查询前驱
int GetPre(int v){
	int id=root,pre;
	while(id){
		if(val[id]<v)pre=val[id],id=ch[id][1];
		else id=ch[id][0];
	}
	return pre;
}
//查询后继
int GetNext(int v){
	int id=root,next=0;
	while(id){
		if(val[id]>v)next=val[id],id=ch[id][0];
		else id=ch[id][1];
	}
	return next;
}

void dfs(int x){
	if(!x)return;
	dfs(ch[x][0]);
	cout<<val[x]<<' ';
	dfs(ch[x][1]);
}

//Treap模板
int main() {
	#ifdef BUG
    freopen("a.in","r",stdin);
    #endif
	Build();
	int n=read();
	int a;
	while(n--){
		//if(a==1||a==2){cout<<' ';dfs(root);cout<<endl;}
		a=read();
		if(a==1)Insert(root,read());
		else if(a==2)Remove(root,read());
		else if(a==3)printf("%d\n",GetRank(root,read())-1);
		else if(a==4)printf("%d\n",GetVal(root,read()+1));
		else if(a==5)printf("%d\n",GetPre(read()));
		else printf("%d\n",GetNext(read()));
	}
}
